<template>
<div class="chat-container">
    <div class="message-list" style="font-size: 15px;">
      <!-- <van-cell class="cell" v-for="message in messages" :key="message.id" :title="message.sender" :label="message.content" label-class="my-label"  /> -->
        <div class="chat-area" style="margin-bottom: 6.5vh;">
            <div class="chatgpt"  v-for="message in messages">
            <div class="line-box">
                    <div :class="[message.chatboxstyle]">
                        <div class="avatar">
                                <img :src="message.pic" alt="Avatar">
                            </div>
                            <div class="content">
                                <div class="header">
                                <p class="name">{{ message.sender }}</p>
                                </div>
                                <div class="message">
                                <p>{{ message.content }}</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>   
        </div>
    
    <div class="input-box">
      <van-field style="border-radius: 2px;" v-model="inputText" placeholder="请输入消息" @keydown.enter="sendMessage" />
      <van-button type="primary" @click="sendMessage">发送</van-button>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import axios from '../api/index';

let avatarList = ['https://pic3.zhimg.com/v2-1f51aa91b8cf176b9e4d6bf8d211bbfe_b.webp','https://pic1.zhimg.com/v2-f19f00dca05e329051da735b359e6934_b.webp','https://pic4.zhimg.com/v2-0c3905e90bef147bbfa0cb0a1dc9056b_b.webp']
let avatarref = avatarList[Math.floor(Math.random() * 3)]
let messages =reactive([
        { id: 1, sender: 'OneByOne', content: 'Hi,我是OneByOne(王百万)' ,pic:'https://picx.zhimg.com/80/v2-c3119402604230588b7a733083ad3674_1440w.webp?source=1940ef5c',chatboxstyle:['bubble-dialog']},
        { id: 2, sender: 'OneByOne', content: 'MyChat是一个人工智能，他能够处理包括但不限于问答，以及您的各种合法问题。',pic:'https://picx.zhimg.com/80/v2-c3119402604230588b7a733083ad3674_1440w.webp?source=1940ef5c',chatboxstyle:['bubble-dialog'] },
        // ...
      ])
let inputText = ref('')
let sendMessage=()=>{
    if (inputText.value) {
        let text = inputText.value
        messages.push({ id: messages.length + 1, sender: 'You', content:text ,pic:"https://picx.zhimg.com/80/v2-20b248bfaaae628af55da96f8b92f256_1440w.webp?source=1940ef5c",chatboxstyle:['bubble-dialog-left']})
        printMessage(text)
        inputText.value = ''
    }
}
let printMessage = (text) => { 
    if (text) {
        axios.get(`/chatgpt1?message=${text}`)
            .then((response) => {
                console.log(response);
                messages.push({ id: response.id, sender: 'ChatGpt', content: response.text, pic: avatarref, chatboxstyle: ['bubble-dialog'] }) 
            })
            .catch((error) => {
                console.error(error);
              messages.push({ id: response.id, sender: 'ChatGpt', content: response.text, pic: avatarref, chatboxstyle: ['bubble-dialog'] }) 
            });
    } else { 
        messages.push({ id: response.id, sender: 'ChatGpt', content: '请输入您的问题', pic:avatarref, chatboxstyle: ['bubble-dialog'] })
    }
}
</script>

<style scoped>
.line-box{
    width: 100vw;
    height: fit-content;
    overflow: hidden;
}

.bubble-dialog {
  /* display: flex;
  align-items: flex-start; */
  background-color: #f5f5f5;
  border-radius: 10px;
  padding: 10px;
  width: fit-content;
  margin: 10px;
}
.bubble-dialog-left{
    background-color: #10ac84;
    border-radius: 10px;
    padding: 10px;
    width: fit-content;
    margin: 10px;
    float: right;
    color: white;
} 
.bubble-dialog-left .avatar{
    float: right;
    margin-right: -2px;
    margin-left: 3px;
}
.avatar {
    float: left;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  margin-right: 10px;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.content {
  overflow: hidden;
  flex-grow: 1;
}

.header {
  display: flex;
  align-items: center;
}
.bubble-dialog-left .name{
    color: white;
}
.bubble-dialog-left .message p{
    color: white;
}
.name {
  float: left;
  margin: 0;
  padding: 0;
  font-size: 16px;
  font-weight: bold;
  color: #333;
}

.message {
  margin-top: 5px;
}

.message p {
  margin: 0;
  padding: 0;
  font-size: 14px;
  color: #666;
  white-space: pre-line;
}




.chat-container {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.message-list {
  flex: 1;
  overflow-y: auto;
}

.input-box {
  display: flex;
  align-items: center;
  padding: 10px;
  position: fixed;
  left: 0;
  bottom: 0;
  width: 96vw;
  height: 5vh;
  background-color:#f5f5f5;
}
</style>